<?php

namespace App\Http\Controllers\Api\V1;
use App\Models\Apple;
use Illuminate\Support\Facades\Cache;
use App\Services\TransferApple\TransferApple;
use App\Services\TransferApple\TransferAppleRequest;
use App\Services\ExchangeBalance\ExchangeBalance;
use App\Services\ExchangeBalance\ExchangeBalanceRequest;
use App\Http\Controllers\Api\V1\Queries\AppleQuery;
use Brick\Math\RoundingMode;

class ApplesController extends Controller
{
	

	public function transfer()
	{

		$lock = Cache::lock('transfer_apples_'.request()->user()->id, 3);
		// 尝试获取锁
		if ($lock->get()) {
		   $request = app(TransferAppleRequest::class);

			$vipPurchase = app(TransferApple::class, [
				'self' => request()->user(),
				'guser' => $request->getGuser(),
				'apples' => request()->apples,
				
			])->apply();
		 
		    // 释放锁
		    $lock->release();
		} else {
		    // 如果不能获取锁，可以给用户友好的提示或者重定向
		   fail('处理中...请勿频繁操作');
		}

		//$lock = Cache::lock('transfer_apples'.request()->user()->id, 3);

		/*try {

			if (! $lock->get()) fail('处理中...请勿频繁操作');

			$request = app(TransferAppleRequest::class);

			$vipPurchase = app(TransferApple::class, [
				'self' => request()->user(),
				'guser' => $request->getGuser(),
				'apples' => request()->apples,
				
			])->apply();

		} finally {
			$lock->release();
		}*/

		return response(null, 204);
	}


	public function exchange()
	{


		$lock = Cache::lock('exchange_balances_'.request()->user()->id, 10);
		// 尝试获取锁
		if ($lock->get()) {
		   $request = app(ExchangeBalanceRequest::class);

			$vipPurchase = app(ExchangeBalance::class, [
				'user' => request()->user(),
				'apples' => request()->apples,
				
			])->apply();
		 
		    // 释放锁
		    $lock->release();
		} else {
		    // 如果不能获取锁，可以给用户友好的提示或者重定向
		   fail('处理中...请勿频繁操作');
		}
		/*$lock = Cache::lock('exchange_balances', 3);

		try {

			if (! $lock->get()) fail('处理中...请勿频繁操作');

			$request = app(ExchangeBalanceRequest::class);

			$vipPurchase = app(ExchangeBalance::class, [
				'user' => request()->user(),
				'apples' => request()->apples,
				
			])->apply();

		} finally {
			$lock->release();
		}*/

		return response(null, 204);
	}

	public function index()
	{
		
		$user = request()->user();
		$data = [];
		for($i=1;$i<=2;$i++){
			$res = [];
			$count = $user->myValidTeam()->where('relation_users.team_level',$i)->count();
			if($count > 0){
				$dividendMoney= '0.00000000';
				$remark = "{$i}_tree_produce_apples";
				if($user->isValidVip()){
					$dividendMoney = $user->userApples()->where('remark',$remark)->sum('money');
					$dividendMoney = big_decimal($dividendMoney)->dividedBy(1,8,RoundingMode::HALF_UP);
				}
				$res['count'] = $user->isValidVip() ?  $count : 0;
				$res['title'] = $i."区苹果树";
				
				$res['dividend_money'] =  $dividendMoney  ??  '0.00000000';
				$res['rate'] =  $user->isValidVip() && empty($user->is_stop) ?  $user->moreLevelTeamDay($i) : '0' ;
				$res['is_open'] = $user->allowedPart($i);
				$data[] = $res;
			}


		}
		return ['data' => $data];
	}

	public function records()
	{ 
		$user = request()->user();
		$condition = request('condition');

		if(!$condition){
			$query = Apple::query();
		}else{

			$query = Apple::query()->where('remark',$condition);
		}

		$query = $query->where('user_id', $user->id);
		$query = !empty(request('start_time')) ? $query->where('created_at', '>', request('start_time')) : $query;
		$query = !empty(request('end_time')) ? $query->where('created_at', '<', request('end_time')) : $query;
	
		$total = $query->sum('money');
		$cards = $query->orderByDesc('id')->paginate();

		
		return ['total' => $total, 'data' => $cards];
	}


	public function config()
	{
		$apple_actual_price = cs("sale")->apple_actual_price;

		$result = [
			'apple_actual_price' => $apple_actual_price,
			
		];
		

		return response()->json($result);
	}
}
